<?php

/**
 * alltosun.com 注册 register.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2015-1-18 下午8:12:12 $
 * $Id$
*/

class Action
{
    private $user_id;

    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();

    }

    /**
     * 用户注册界面
     */
    public function __call($action= '', $param = array())
    {
        if ($this->user_id) {
            Response::redirect(AnUrl('user/setting'));
            return false;
        }
        display_template('register.html');
        // if(is_mobile()) {
        //     Response::display('m/register.html');
        // } else {
        //     Response::display('register.html');    
        // }
    }

    public function glassRegister()
    {
        Response::display('m/glassRegister.html');
    }

    //update zhangyh 对GLASS嵌套页面进行注册流程的修改，增加ajax返回信息。

    public function save()
    {
        //如果已经登录，就跳转
        if($this->user_id) {
            Response::redirect(AnUrl('user/setting'));
            return false;
        }
        $username = trim(Request::Post('username', ''));
        $email    = trim(Request::Post('email', ''));
        $mobile   = trim(Request::Post('mobile', ''));
        $code     = trim(Request::Post('code', ''));
        $pw       = trim(Request::Post('password', ''));
        $repw     = trim(Request::Post('repassword', ''));
        $hash     = user_helper::random_hash(6);
        if(!user_helper::is_username_len($username)) {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'您提交的用户名长度不符合要求！');
            }
            return array('您提交的用户名长度不符合要求！', 'error', AnUrl('user/register'));
        }
        if(user_helper::is_email($username)) {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'您提交的用户名不能是邮箱！');
            }
            return array('您提交的用户名不能是邮箱！', 'error', AnUrl('user/register'));
        }
        if(!user_helper::is_username_unique($username)) {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'您提交的用户名已经注册！');
            }
            return array('您提交的用户名已经注册！', 'error', AnUrl('user/register'));
        }
        if(user_helper::is_number($username)) {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'您提交的用户名不能为纯数字！');
            }
            return array('您提交的用户名不能为纯数字！', 'error', AnUrl('user/register'));
        }
        $info = array('user_name' => $username);
        if(!empty($mobile)){
            if(!user_helper::is_mobile($mobile)) {
                if(is_mobile() && Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>'您提交的手机号的格式不正确！');
                }
                return array('您提交的手机号的格式不正确！', 'error', AnUrl('user/register'));
            }
            if(!user_helper::is_mobile_unique($mobile)) {
                if(is_mobile() && Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>'您提交的手机号已经被注册！');
                }
                return array('您提交的手机号已经被注册！', 'error', AnUrl('user/register'));
            }
            if(empty($code)) {
                if(is_mobile() && Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>'您提交的验证码已经不能为空！');
                }
                return array('您提交的验证码已经不能为空！', 'error', AnUrl('user/register'));
            }
            $return_code = user_helper::getMobileCode($mobile, 'register');
            if($return_code['code']) {
                if($code != $return_code['code']) {
                    if(is_mobile() && Request::isAjax()) {
                        return array('info'=>'error', 'msg'=>'您提交的手机验证码不正确，请重新发送！');
                    }
                    return array('您提交的手机验证码不正确，请重新发送！', 'error', AnUrl('user/register'));
                }
            } else {
                if(is_mobile() && Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>'您提交的手机验证码不正确，请重新发送！');
                }
                return array('您提交的手机验证码不正确，请重新发送！', 'error', AnUrl('user/register'));
            }
            _model('mobile_code')->update(array('mobile' => $mobile, 'code' => $code, 'res_name' => 'register'), array('status' => 1));
            $info['mobile'] = $mobile;
        } elseif(!empty($email)) {
            if(!user_helper::is_email($email)) {
                return array('您提交的邮箱格式不正确！', 'error', AnUrl('user/register'));
            }
            if(!user_helper::is_email_unique($email)) {
                return array('您提交的邮箱已经被注册！', 'error', AnUrl('user/register'));
            }
            $info['email'] = $email;
        } else {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'您提交的数据有误！');
            }
            return array('您提交的数据有误！', 'error', AnUrl('user/register'));
        }
        if(is_mobile()) {
            if(empty($pw)) {
                if(Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>'您提交的密码不能为空！');
                }
                return array('您提交的密码不能为空！', 'error', AnUrl('user/register'));
            }
        } else {
            if(empty($pw) || empty($repw)) {
                return array('您提交的密码不能为空！', 'error', AnUrl('user/register'));
            }    
        }
        
        if(user_helper::is_pwd_null($pw)) {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'您提交的密码内不能包含空字符！');
            }
            return array('您提交的密码内不能包含空字符！', 'error', AnUrl('user/register'));
        } else {
            if(!user_helper::is_strlen_scope($pw, 6, 20)) {
                if(is_mobile() && Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>'您提交的密码长度不符合要求！');
                }
                return array('您提交的密码长度不符合要求！', 'error', AnUrl('user/register'));
            }
        }
        if(!is_mobile()) {
            if($pw != $repw) {
                return array('您提交的两次输入的密码不一致。', 'error', AnUrl('user/register'));
            }    
        }
        
        $info['password'] = user_helper::md5_password($pw, $hash);
        $info['hash']     = $hash;
        //针对glass嵌套页面注册进行标识
        if(is_mobile() && Request::isAjax()) {
            $info['user_from']     = 'glass';
        }
        $user_id = _model('user')->create($info);
        if($user_id) {
            _model('role_user')->create(array('user_id' => $user_id, 'role_id' => '2'));
            $user_info = _uri('user', $user_id);
            user_helper::session_update($user_info);
            //ucenter注册
            if(isset($info['mobile'])) {
                $return_info = user_helper::uc_register($info['user_name'], $pw, '', '', $info['mobile']);
            } elseif(isset($info['email'])) {
                $return_info = user_helper::uc_register($info['user_name'], $pw, '', '', '', 8);
            }
            if($return_info['info'] == 'ok') {
                _model('user')->update($user_id, array('uc_id' => $return_info['uc_id']));
                if(isset($info['email'])) {
                    $callback = AnUrl('user/register/send_email?type=register');
                } else {
                    $callback = user_helper::get_jump_url();
                }
                $ucenter_info = user_helper::uc_login($info['user_name'], $pw, $callback);
                if($ucenter_info['info'] == 'error') {
                    if(is_mobile() && Request::isAjax()) {
                        return array('info'=>'error', 'msg'=>$ucenter_info['msg']);
                    }
                    return array($ucenter_info['msg'], 'error', AnUrl('user/register'));
                } else {
                    if(is_mobile() && Request::isAjax()) {
                        return array('info'=>'ok', 'jump_url'=>$callback);
                    }
                    echo $ucenter_info;
                }
            } else {
                _model('user')->delete(array('id'=>$user_id)); 
                _model('role_user')->delete(array('user_id' => $user_id));
                if(is_mobile() && Request::isAjax()) {
                    return array('info'=>'error', 'msg'=>$return_info['msg']);
                }
                return array($return_info['msg'], 'error', AnUrl('user/register'));
            }
        } else {
            if(is_mobile() && Request::isAjax()) {
                return array('info'=>'error', 'msg'=>'不好意思，注册失败，请重新注册。');
            }
            return array('不好意思，注册失败，请重新注册。', 'error', AnUrl('user/register'));
        }
    }
    
    public function ajax_mobile()
    {
        $mobile   = trim(Request::Post('mobile', ''));
        if(empty($mobile)) {
            return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机号不能为空！');
        }
        if(!user_helper::is_mobile($mobile)) {
            return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机号的格式不正确！');
        }
        if(!user_helper::is_mobile_unique($mobile)) {
            return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机号已经被注册！');
        }
        return array('info' => 'succeed');
    }
    
    public function ajax_email(){
        $email    = trim(Request::Post('email', ''));
        if(empty($email)) {
            return array('info' => 'error', 'type'=>'email','msg' => '您填写的邮箱不能为空！');
        }
        if(!user_helper::is_email($email)) {
            return array('info' => 'error', 'type'=>'email','msg' => '您填写的邮箱格式不正确！');
        }
        if(!user_helper::is_email_unique($email)) {
            return array('info' => 'error', 'type'=>'email','msg' => '您填写的邮箱已经被注册！');
        }
        return array('info' => 'succeed');
    }
    
    public function ajax_username(){
        $username = trim(Request::Post('username', ''));
        $user_id  = trim(Request::Post('user_id', 0));

        if(empty($username)) {
            return array('info' => 'error', 'type'=>'username','msg' => '您填写的用户名不能为空！');
        }
        if(!user_helper::is_username_len($username)) {
            return array('info' => 'error', 'type'=>'username','msg' => '您填写的用户名格式不正确！');
        }
        if(!user_helper::is_username_unique($username, $user_id)) {
            return array('info' => 'error', 'type'=>'username','msg' => '您填写的用户名已经注册！');
        }
        return array('info' => 'succeed');
    }
    
    public function ajax_code(){
        $mobile   = trim(Request::Post('mobile', ''));
        $code     = trim(Request::Post('code', ''));
        $code_type = trim(Request::Post('code_type', ''));
        if(empty($mobile)) {
            return array('info' => 'error', 'type'=>'mobile','msg' => '您填写的手机号不能为空！');
        }
        if(empty($code)) {
            return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机验证码不能为空！');
        }
        $return_code = user_helper::getMobileCode($mobile, $code_type);
        if($return_code['times'] + 600 < time()) {
            return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机验证码已过期，请重新发送！');
        }
        if($return_code['code']) {
            if($code != $return_code['code']) {
                return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机验证码不正确！');
            }
        } else {
            return array('info' => 'error', 'type'=>'code','msg' => '您填写的手机验证码不正确,请重新发送！');
        }
        return array('info' => 'succeed');
    }
    
    /**
     * 发送短信
     */
    public function sendRegsms() {
        $mobile = trim(Request::Post('mobile', ''));
        $send_type = trim(Request::Post('send_type', ''));
        if(empty($send_type) || !in_array($send_type, sms_config::$send_sms_type)) {
            return array('info' => 'error', 'type'=>'code','msg' => '参数错误');
        }
        
        $code_info = _model('mobile_code')->read(array('mobile' => $mobile), 'ORDER BY `id` DESC');

        if($code_info && $code_info['times']+10 > time()) {
            return array('info' => 'error', 'type'=>'code','msg' => '短信发送成功，请不要重复点击！');
        }
        
        $return_info = sms_helper::sendRegSms($mobile, $send_type);
        if(!$return_info) {
            return array('info' => 'error', 'type'=>'code', 'msg' => '短信发送失败！');
        } else {
            return array('info' => 'succeed', 'type'=>'code', 'msg' => '短信发送成功！');
        }
    }
    
    public function send_email() {
        if (!$this->user_id) {
            Response::redirect(AnUrl('user/login'));
            return false;
        }
        $type = Request::Get('type', '');
        if(empty($type) || !in_array($type, mail_config::$send_mail_type)) {
            return array('参数错误！', 'error', AnUrl('user/setting'));
        }
        $email_info = _model('email_code')->read(array('user_id' => $this->user_id), 'ORDER BY `id` DESC');
        if($email_info && $email_info['status'] == '0' && $email_info['times'] + mail_config::$mail_time > time()) {
            return array('邮箱验证码已经发送成功！', 'success', AnUrl('user/setting'));
        }
        $user_info = _uri('user', $this->user_id);
        user_helper::set_mail_send($user_info['email'], $this->user_id, $type);
        Response::assign('type', $type);
        Response::assign('email', $user_info['email']);
        Response::display('sendmail.html');
    }
    
    /**
     * 验证邮箱
     */
    public function check_email(){
        $code  = Request::Get('code', '');
        $email = Request::Get('email', '');
        $type  = Request::Get('type', '');
        if($code == '' || $email == '' || $type == '' || !in_array($type, mail_config::$send_mail_type)) {
            return array('邮箱验证链接已经失效【1】', 'error', AnUrl('user/setting'));
        }

        $code_info = _uri('email_code', array('code' => $code, 'email' => $email, 'res_name' => $type));

        $errorCallBack = mail_config::$send_mail_content[$type]['errorCallBack'];
        $successCallBack = mail_config::$send_mail_content[$type]['successCallBack'];

        if(!$code_info || !is_array($code_info) || $code_info['status']){
            return array('邮箱验证链接已经失效【2】', 'error', AnUrl($errorCallBack));
        }

        $user_info = _uri('user', $code_info['user_id']);
        //如果是修改邮箱的话  
        if($type == 'edit') {
            $user_info['email'] = $code_info['email'];
        }
        if($user_info['email'] != $code_info['email']) {
            return array('邮箱验证链接已经失效【3】', 'error', AnUrl($errorCallBack));
        }
        if($code_info['times'] + mail_config::$mail_time < time()) {
            return array('邮箱验证链接已经失效【4】', 'error', AnUrl($errorCallBack));
        }
        
        $return_info = user_helper::uc_edit($user_info['uc_id'], $user_info['user_name'], $user_info['email'], '', '', 10);
        if($return_info['info'] != 'ok') {
            return array('ucenter:'.$return_info['msg'], 'error', AnUrl($errorCallBack));
        } else {
            $filter = array(
                'is_check_email' => 1,
                'email' => $user_info['email'],
                'is_lenovo_employees' => 0
            );
            //如果为联想的员工，则在数据库中标识一下
            $employees_email = user_config::$employees_email;
            foreach ($employees_email as $k => $v) {
                if(strrchr($user_info['email'], $v) == $v) {
                    $filter['is_lenovo_employees'] = 1;
                }
            }
            _model('user')->update($user_info['id'], $filter);
            _model('email_code')->update($code_info['id'], array('status' => 1));
            if(!$this->user_id) {
                if($type == 'getback') {
                    Response::redirect(AnUrl($successCallBack.'?code='.$code.'&email='.$email.'&type='.$type));
                    return;
                }
                return array('邮箱验证成功!', 'success' , AnUrl('user/login'));
            } else {
                return array('邮箱验证成功!', 'success' , AnUrl($successCallBack));
            }
        }
    }
}
?>